package com.ys.controller.api.sys;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ys.dto.DtreeSqlDto;
import com.ys.dto.YsTreeDto;
import com.ys.entity.sys.TsOperateOrg;
import com.ys.service.sys.CommonApiService;
import com.ys.service.sys.TsOperateOrgService;
import com.ys.vo.LayuiTableVoJA;
import com.ys.vo.R;

@Controller
@RequestMapping("/api/inter/tsOperateOrg")
public class ApiInterTsOperateOrgController {
	
	@Autowired
	private CommonApiService<TsOperateOrg> commonApiService;
	@Autowired
	private TsOperateOrgService tsOperateOrgService;

	@RequiresPermissions("tsOperateOrg:view")
	@RequestMapping("/search")
    @ResponseBody
	public LayuiTableVoJA search(
			@ModelAttribute TsOperateOrg cond,
			Long nodeId
			){
		if (nodeId != null) {
			TsOperateOrg node = new TsOperateOrg().setId(nodeId).queryFirst();
			cond.where("A.NodeLft >= " + node.getNodeLft())
			.where("A.NodeRgt <= " + node.getNodeRgt());
		}
		
		cond.addLeftJoin("TS_OperateOrg B on B.id = A.Pid")
			.addField("B.name pname");
		
		cond.addLeftJoin("THR_OperateCompanyApply C on C.VGsBm = A.HeadVgsbm")
			.addField("C.VGsJc headVgsjc");



		
		return commonApiService.searchLay(cond);
	}


	/**
	 * 弹出框
	 */
	@RequestMapping("/searchForSelect")
    @ResponseBody
	public LayuiTableVoJA searchForSelect(@ModelAttribute TsOperateOrg cond){
		cond.addField("P.Name AS pName");
		cond.addField("PO.Vgsbm AS parentVgsbm");
		cond.addField("PO.Vgsjc AS parentVgsjc");
		cond.addLeftJoin("TS_OperateOrg P on P.Id = A.Pid");
		cond.addLeftJoin("THR_OperateCompanyApply PO ON A.HeadVgsbm = PO.Vgsbm AND 1=1");
		cond.setStatus(1);
		//LayuiTableVoJA tableVoJA = commonApiService.searchLay(cond);
		return commonApiService.searchLay(cond);
	}
	
	@RequestMapping("/dtree")
    @ResponseBody
	public DtreeSqlDto dtree(){
		return tsOperateOrgService.dtree();
	}
	
	@RequestMapping("/ysTree")
    @ResponseBody
	public YsTreeDto ysTree(){
		return tsOperateOrgService.ysTree();
	}
	
	/**
	 * 新增
	 */
	@PostMapping("/add")
    @ResponseBody
	public R add(Long pid, String name){
		
		return tsOperateOrgService.add(pid, name);
	}
	
	/**
	 * 编辑
	 */
	@PostMapping("/edit")
	@RequiresPermissions("tsOperateOrg:edit")
    @ResponseBody
	public R edit(Long id, String name, String headVgsbm, int status){
		
		return tsOperateOrgService.edit(id, name, headVgsbm,status);
	}






}
